//找到字符串中所有字母异位词

class Solution {
public:
    vector<int> findAnagrams(string s, string p) 
    {
        vector<int>res;
        int hashp[26]={0};
        for(auto ch:p) hashp[ch-'a']++;
        int hashs[26]={0};
        int count=0;//记录有效字母个数
        //比如实例2 count=2 
        //控制count的正确性即可 即只有当出现次数小于等于hashp中的次数时 才++
        for(int l=0,r=0;r<s.size();r++)
        {
            char in=s[r];
            if(++hashs[in-'a'] <= hashp[in-'a']) count++;
            while(r-l+1>p.size())
            {
                char out=s[l];
                l++;
                if(--hashs[out-'a'] < hashp[out-'a'] ) count--;
            }
            if(count==p.size())
            {
                res.push_back(l);
            }
        }
        return res;
    }
};
//将 x 减到 0 的最小操作数

class Solution {
public:
    int minOperations(vector<int>& nums, int x) 
    {
        int sum=0;
        for(auto e:nums) sum+=e;
        int target=sum-x;
        if(target < 0 ) return -1;
        if(target==0) return nums.size();
        int tmp=0;
        int length=0;
        for(int l=0,r=0;r<nums.size();r++)
        {
            tmp+=nums[r];
            while(tmp>target)
            {
                tmp-=nums[l++];
            }
            if(tmp==target)
            {
                length=max(length,r-l+1);
            }
        }
        return length==0?-1:nums.size()-length;
    }
};